﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Domain.Entity.ProductManage;
using NFine.Domain._03_Entity.InformationCenter;
using NFine.Domain._04_IRepository.InformationCenter;
using NFine.Repository.InformationCenter;

namespace NFine.Application.InformationCenter
{
    public class SysNewsApp
    {

        private ISysNewsRepository newsService = new SysNewsRepository();

        public List<SysNewsEntity> GetList(Pagination pagination, string keyword = "")
        {
            var expression = ExtLinq.True<SysNewsEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Title.Contains(keyword));
            }

            return newsService.FindList(expression, pagination).OrderByDescending(a => a.F_CreatorTime).ToList();
        }

        public List<SysNewsEntity> GetLatelyNews(int typeId)
        {
            var expression = ExtLinq.True<SysNewsEntity>();
            List<SysNewsEntity> list = new List<SysNewsEntity>();
            string sql = string.Format("select * from sys_news where F_TypeId = {0}", typeId);
            list = newsService.FindList(sql).OrderByDescending(a => a.F_CreatorTime).Take(22).ToList();
            return list;
        }

        public SysNewsEntity GetForm(string keyValue)
        {
            return newsService.FindEntity(keyValue);
        }

        public void SubmitForm(SysNewsEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                newsService.Update(entity);
            }
            else
            {
                entity.Create();
                newsService.Insert(entity);
            }
        }

        public int DeleteNews(string keyValues)
        {
            return newsService.DeleteNews(keyValues);
        }

    }
}
